package com.spartez.farhorizons.client;

import java.util.ArrayList;
import java.util.Collection;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.spartez.farhorizons.client.data.ISystemDescr;

public class DataService {
	private final GreetingServiceAsync remoteService = GWT.create(GreetingService.class);
	private ISystemDescr selectedSystem;
	private ISystemDescr hoveredSystem;
	private int currentGravity = 10;
	
	
	public interface StarListListener {
		void starListChanged(Collection<? extends ISystemDescr> result);
	}
	
	public interface SystemSelectedListener {
		void systemSelected(ISystemDescr selectedSystem);
	}

	public interface SystemHoveredListener {
		void systemHovered(ISystemDescr hoveredSystem);
	}

	public interface GravityTechLevelListener {
		void gravityTechLevel(int gravity);
	}

	private final Collection<StarListListener> starListListeners = new ArrayList<StarListListener>();
	private final Collection<SystemSelectedListener> systemSelectedListeners = new ArrayList<SystemSelectedListener>();
	private final Collection<SystemHoveredListener> systemHoveredListeners = new ArrayList<SystemHoveredListener>();
	private final Collection<GravityTechLevelListener> gravityTechLevelListeners = new ArrayList<GravityTechLevelListener>();
	
	public void registerStarListListener(StarListListener listener) {
		starListListeners.add(listener);
	}
	
	public void registerSystemSelectedListener(SystemSelectedListener listener) {
		systemSelectedListeners.add(listener);
	}

	public void registerSystemHoveredListener(SystemHoveredListener listener) {
		systemHoveredListeners.add(listener);
	}

	public void registerGravityTechLevelListener(GravityTechLevelListener listener) {
		gravityTechLevelListeners.add(listener);
	}

	public void setSelectedSystem(ISystemDescr system) {
		selectedSystem = system;
		for (SystemSelectedListener listener : systemSelectedListeners) {
			listener.systemSelected(system);
		}
	}
	
	public void setHoveredSystem(ISystemDescr system) {
		hoveredSystem = system;
		for (SystemHoveredListener listener : systemHoveredListeners) {
			listener.systemHovered(system);
		}
	}

	public ISystemDescr getSelectedSystem() {
		return selectedSystem;
	}

	public void setStarList(String list) {
		remoteService.parseStarList(list, new AsyncCallback<Collection<? extends ISystemDescr>>() {

			public void onFailure(Throwable caught) {
			}

			public void onSuccess(Collection<? extends ISystemDescr> result) {
				for (StarListListener listener: starListListeners) {
					listener.starListChanged(result);
				}
			}
			
		});
	}
	
	public int getCurrentGravity() {
		return currentGravity;
	}

	public ISystemDescr getHoveredSystem() {
		return hoveredSystem;
	}

	public void loadData() {
		remoteService.getStarList(new AsyncCallback<Collection<? extends ISystemDescr>>() {

			public void onFailure(Throwable caught) {
			}

			public void onSuccess(Collection<? extends ISystemDescr> result) {
				if (result != null && !result.isEmpty()) {
					for (StarListListener listener: starListListeners) {
						listener.starListChanged(result);
					}
				}
			}
			
		});
		
	}
	
	public void setGravityTechLevel(int grav) {
		currentGravity = grav;
		for (GravityTechLevelListener listener : gravityTechLevelListeners) {
			listener.gravityTechLevel(grav);
		}
	}
}
